package com.cw.jeeyt.service.impl.process;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.process.ActDeModelRelationBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.process.ActDeModelRelationDao;
import com.cw.jeeyt.dao.model.process.ActDeModelRelationDo;
import com.cw.jeeyt.service.converter.process.ActDeModelRelationConverter;
import com.cw.jeeyt.service.intf.process.ActDeModelRelationService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 流程定义模型关联业务实现类
 *
 * @author chenw
 * @date 2019-03-10 21:47:04
 */
@Service
public class ActDeModelRelationServiceImpl implements ActDeModelRelationService {

    private static final Logger logger = LoggerFactory.getLogger(ActDeModelRelationServiceImpl.class);
    @Autowired
    private ActDeModelRelationDao actDeModelRelationDao;//流程定义模型关联数据库操作接口

    /**
     * 查询所有流程定义模型关联
     */
    @Override
    public List<ActDeModelRelationBo> queryActDeModelRelationList() {
        return ActDeModelRelationConverter.doList2BoList(actDeModelRelationDao.queryActDeModelRelationList());
    }

    /**
     * 分页查询流程定义模型关联
     *
     * @param actDeModelRelationBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ActDeModelRelationBo> queryByPage(ActDeModelRelationBo actDeModelRelationBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ActDeModelRelationDo> pageInfo = actDeModelRelationDao.queryByPage(
            ActDeModelRelationConverter.bo2Do(actDeModelRelationBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ActDeModelRelationConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询流程定义模型关联信息
     *
     * @return
     */
    @Override
    public ActDeModelRelationBo getActDeModelRelationById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return ActDeModelRelationConverter.do2Bo(actDeModelRelationDao.getById(id));
    }

    /**
     * 新增流程定义模型关联
     *
     * @param actDeModelRelationBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ActDeModelRelationBo actDeModelRelationBo) throws Exception {

        if (actDeModelRelationBo == null) {
            logger.error("流程定义模型关联为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelRelationDo actDeModelRelation = ActDeModelRelationConverter.bo2Do(actDeModelRelationBo);
        actDeModelRelation.setId(SequenceTool.nextId());
        Integer result = actDeModelRelationDao.insert(actDeModelRelation);
        return result;
    }

    /**
     * 新增流程定义模型关联不为空的字段
     *
     * @param actDeModelRelationBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ActDeModelRelationBo actDeModelRelationBo) throws Exception {

        if (actDeModelRelationBo == null) {
            logger.error("流程定义模型关联为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelRelationDo actDeModelRelation = ActDeModelRelationConverter.bo2Do(actDeModelRelationBo);
        actDeModelRelation.setId(SequenceTool.nextId());
        Integer result = actDeModelRelationDao.insertSelective(actDeModelRelation);
        return result;
    }

    /**
     * 更新流程定义模型关联
     *
     * @param actDeModelRelationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ActDeModelRelationBo actDeModelRelationBo) throws Exception {

        if (actDeModelRelationBo == null) {
            logger.error("流程定义模型关联为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelRelationDo actDeModelRelation = ActDeModelRelationConverter.bo2Do(actDeModelRelationBo);

        actDeModelRelationDao.update(actDeModelRelation);
    }

    /**
     * 更新流程定义模型关联不为空的字段
     *
     * @param actDeModelRelationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ActDeModelRelationBo actDeModelRelationBo) throws Exception {

        if (actDeModelRelationBo == null) {
            logger.error("流程定义模型关联为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelRelationDo actDeModelRelation = ActDeModelRelationConverter.bo2Do(actDeModelRelationBo);

        actDeModelRelationDao.updateSelective(actDeModelRelation);
    }

    /**
     * 删除流程定义模型关联
     *
     * @param actDeModelRelationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ActDeModelRelationBo actDeModelRelationBo) throws Exception {

        if (actDeModelRelationBo == null) {
            logger.error("流程定义模型关联为空");
            return;
        }
        ActDeModelRelationDo actDeModelRelation = ActDeModelRelationConverter.bo2Do(actDeModelRelationBo);
        actDeModelRelationDao.delete(actDeModelRelation);
    }
}
